package com.foxzzz.two.pointers;

public class Solution142 {

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode detectCycle(ListNode head) {
        if (head == null)
            return null;
        ListNode slow = head;
        ListNode qucik = head;
        while (true) {
            slow = slow.next;
            qucik = qucik.next;
            if (qucik == null) {
                return null;
            }
            qucik = qucik.next;
            if (qucik == null) {
                return null;
            }
            if (slow == qucik) {
                slow = head;
                while (slow != qucik) {
                    slow = slow.next;
                    qucik = qucik.next;
                }
                return slow;
            }
        }
    }
}
